const app = getApp();

Page({

  data: {
    student: null,
    imageList: [""],
    imgs : [],
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'RichText组件'
      }]
    }]
  },


  formSubmit: function (e) {
    let _this = this;
    wx.uploadFile({
      url: 'https://dorm.dormassistant.wang:8080/message/addMessage', //仅为示例，非真实的接口地址
      filePath: _this.data.imageList[0],
      name: "image",
      formData: {
        userId: wx.getStorageSync('openid'),
        title: e.detail.value.title,
        text: e.detail.value.text
      },
      header: {
        'content-type': "multipart/form-data"
      },
      success: (res) => {
        console.log(res);

        if (res.data.success )
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  url: "./socialContact/socialContact"
                })
              }, 2000)
            }
          });

      }
    })
  },


  chooseImage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })

  },


  // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 1,
      success: function (res) {
        if (res.tempFiles[0].size > 750000) {
          wx.showToast({
            title: "图片过大",
            icon: '',
            duration: 2000
          });
          return;
        }
        wx.uploadFile({
          url: 'http://localhost:8080/message/uploadImage',
          filePath: res.tempFilePaths[0],
          name: "image",
          method:'POST',
          formData: {
            'user': 'test'
          },
          success:function(res){
            console.log(res.data)
            var imageSize = that.data.imageList.length
            that.editorCtx.insertImage({
              src: res.data.imageUrl,
            })
          }
        })
        that.setData({
          imageList: res.tempFilePaths
        })
      }
    })
  },

  /**
   * 图片预览
   */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },

  // 初始化编辑器
  onEditorReady() {
    var that=this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
      if (wx.getStorageSync("content")) { // 设置~历史值
        that.editorCtx.insertText(wx.getStorageSync("content")) // 注意：插入的是对象
      }

    }).exec()
  },
  // 返回选区已设置的样式
  onStatusChange(e) {
    // console.log(e.detail)
    const formats = e.detail
    this.setData({
      formats
    })
  },
  // 内容发生改变
  onContentChange(e) {
    //var that = this
    // console.log("内容改变")
    // console.log(e.detail)
    // that.setData({
    //   content: e.detail
    // })
    // wx.setStorageSync("content", e.detail)

  },
  // 失去焦点
  onNoFocus(e) {
    //var that = this
    // console.log("失去焦点")
    // console.log(e.detail)
    // that.setData({
    //   content: e.detail
    // })
    // wx.setStorageSync("content", e.detail)
  },
  // 获取内容
  clickLogText(e) {
    var that = this
    that.editorCtx.getContents({
      success: function (res) {
        console.log(res)
        wx.setStorageSync("content", res.html); // 缓存本地
        // < p > 备注说明：</p > <p>1、评分规则</p> <p>2、注意事项</p> <p>3、哈哈呵呵</p> <p><br></p><p><br></p>
        that.setData({
          content: res.html,
          nodes: res.html,
          content_html: res.html
        })
      }
    })
  },
  // 清空所有
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("清空成功")
      }
    })
  },
  // 清除样式
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  // 记录样式
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)
  },
})



